package com.kangye.learnteacher.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kangye.learnteacher.entity.QuestionBank;
import com.kangye.learnteacher.entity.StudentPaper;
import com.kangye.learnteacher.entity.Subject;
import com.kangye.learnteacher.mapper.PaperMapper;
import com.kangye.learnteacher.service.PaperService;
import com.kangye.learnteacher.util.RedisStorageUtil;
import com.kangye.learnteacher.util.ViewJsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @program: learndeduo
 * @description: 试卷逻辑实现类
 * @author: 冯钧豪
 * @create: 2021-05-17 19:12
 **/
@Service
public class PaperServiceImpl implements PaperService {
    /**
     * 注入mapper层
     */
    @Autowired
    private PaperMapper paperMapper;
    /**
     * 注入viewJsonObject 工具类
     */
    @Autowired
    private ViewJsonObject<StudentPaper> viewJsonObject;
    /**
     *  注入redisStorageUtil工具类
     */
    @Autowired
    private RedisStorageUtil redisStorageUtil;

    /**
     *
     * @param spId
     * @param classesName
     * @return 返回工具类
     */
    @Override
    public ViewJsonObject<StudentPaper> selPaper(Integer spId, String classesName) {
        List<StudentPaper> list = paperMapper.selPaper(spId,classesName);
        PageInfo<StudentPaper> pageInfo = new PageInfo<>(list);
        if (list != null) {
            viewJsonObject.setCode(0);
            viewJsonObject.setData(pageInfo.getList());
            viewJsonObject.setCount(pageInfo.getTotal());
            viewJsonObject.setMsg("");
            return viewJsonObject;
        }
        return null;
    }

    /**
     *
     * @param spId
     * @return 删除返回数字 code 大于0表示成功
     */
    @Override
    public Integer delPaper(Integer spId) {
        Integer code = paperMapper.delPaper(spId);
        System.out.println(code);
        if (code > 0) {
            return code;
        }
        return 0;
    }
    @Override
    public List<Map<String,Object>> updPaper(Integer spId) {
//        StudentPaper code = paperMapper.updPaper(spId);
        List<Map<String,Object>> maps = paperMapper.updPaper(spId);
        System.out.println(maps);
        redisStorageUtil.addObjOne("code",maps,5,TimeUnit.MINUTES);
        Object keyOne = redisStorageUtil.getKeyOne("code");
        System.out.println("redis..."+keyOne);
            return maps;
    }

    @Override
    public List XkSelAll() {
        List<Subject> list = paperMapper.XkSelAll();
        for (Subject s: list
             ) {
            System.out.println(s);
        }
        return list;
    }

    @Override
    public ViewJsonObject  SelBank() {
        List  list = paperMapper.SelBank();
        PageInfo pageInfo = new PageInfo<>(list);
        if (list != null) {
            viewJsonObject.setCode(0);
            viewJsonObject.setData(pageInfo.getList());
            viewJsonObject.setCount(pageInfo.getTotal());
            viewJsonObject.setMsg("");
            return viewJsonObject;
        }
        return null;
    }

    @Override
    public Integer AddPaper(StudentPaper studentPaper) {
        Integer code = paperMapper.AddPaper(studentPaper);
        System.out.println(code);
        return code;
    }

    @Override
    public QuestionBank AddPaperUl(Integer qbId) {
        return paperMapper.AddPaperUl(qbId);
    }
}

